﻿using rmTest.Common.ModelUtil;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace rmTest.Common
{
    public class ExpressionTreeVisualizer
    {
        public static void Show()
        {
            {
                Expression<Func<SqlDataReader, object>> lambda = p => p[1] is DBNull ? null : p[1];
                //{
                //    Type type = typeof(BaseModel);
                //    BaseModel t = Activator.CreateInstance<BaseModel>();
                //    PropertyInfo[] propertys = type.GetProperties();
                //    foreach (PropertyInfo property in propertys)
                //    {
                //        property.SetValue(t, p[1] is DBNull ? null : p[1]);
                //    }
                //    return t;
                //};// p[1] is DBNull ? null : p[1];
            }
            {
                //ParameterExpression parameterExpression;
                //Expression<Func<SqlDataReader, BaseModel>> expression = Expression.Lambda<Func<SqlDataReader, BaseModel>>(Expression.MemberInit(Expression.New(typeof(BaseModel)), new MemberBinding[]
                //{
                //Expression.Bind(methodof(BaseModel.set_Id(int)), Expression.Call(null, methodof(int.Parse(string)), new Expression[]
                //{
                //    Expression.Call(Expression.Call(parameterExpression, methodof(DbDataReader.get_Item(string)), new Expression[]
                //    {
                //        Expression.Constant("Id", typeof(string))
                //    }), methodof(object.ToString()), Array.Empty<Expression>())
                //}))
                //}), new ParameterExpression[]
                //{
                //parameterExpression
                //});
            }
            //{
            //    var setMethod = typeof(BaseModel).GetProperty("Id").GetSetMethod();
            //    var intParseMethod = typeof(int).GetMethod("Parse");
            //    //typeof(DbDataReader).

            //    ParameterExpression parameterExpression;
            //    Expression<Func<SqlDataReader, BaseModel>> expression =
            //        Expression.Lambda<Func<SqlDataReader, BaseModel>>(
            //            Expression.MemberInit(Expression.New(typeof(BaseModel)),
            //            new MemberBinding[]
            //    {
            //        Expression.Bind(setMethod,
            //        Expression.Call(null,
            //                        intParseMethod,
            //                        new Expression[]
            //                        {
            //                            Expression.Call(
            //                                Expression.Call(
            //                                    parameterExpression,
            //                                    methodof(DbDataReader.get_Item(string)),
            //                                    new Expression[]
            //                                    {
            //                                        Expression.Constant("Id", typeof(string))
            //                                    }),
            //                                methodof(object.ToString()),
            //                                Array.Empty<Expression>()
            //                            )
            //                        }))
            //                        }), new ParameterExpression[]
            //                        {
            //        parameterExpression
            //    });
            //}
        }

    }
}
